using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FoodMaker : MonoBehaviour
{
    private static FoodMaker _instance;
    public static FoodMaker Instance
    {
        get {
            return _instance;
        }
    }
    
    public int xlimit = 21;
    public int ylimit = 11;
    public int xoffset = 7;

    public GameObject foodPrefab;
    public GameObject rewardPrefab;

    public Sprite[] foodSprites;
    private Transform foodHolder;
    

    private void Awake()
    {
        _instance = this;
    }


    private void Start()
    {
        foodHolder = GameObject.Find("FoodHolder").transform;
        MakeFood(false);
    }

    public void MakeFood(bool isReward) {
        int index = Random.Range(0, foodSprites.Length);
        GameObject food = Instantiate(foodPrefab);

        food.GetComponent<Image>().sprite = foodSprites[index];

        food.transform.SetParent(foodHolder, false);
        int x = Mathf.Clamp(Random.Range(-xlimit + xoffset, xlimit + xoffset + 1), -xlimit, xlimit);
        int y = Mathf.Clamp(Random.Range(-ylimit, ylimit + 1), -ylimit, ylimit);
        food.transform.localPosition = new Vector3(x * 20,y * 20, 0);
        if (isReward) {
            GameObject reward = Instantiate(rewardPrefab);
            reward.transform.SetParent(foodHolder, false);
            x = Mathf.Clamp(Random.Range(-xlimit + xoffset, xlimit + xoffset + 1), -xlimit, xlimit);
            y = Mathf.Clamp(Random.Range(-ylimit, ylimit + 1), -ylimit, ylimit);
            reward.transform.localPosition = new Vector3(x * 20, y * 20, 0);
        }
    }
}
